package csu.rjxyg.service.impl;

import java.util.List;

import csu.rjxyg.action.constant.CourseConstant;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseList;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.dao.BaseDaoAdapter;
import csu.rjxyg.factory.core.BeanFactory;
import csu.rjxyg.factory.core.DaoFactory;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseCourseService;
import csu.rjxyg.util.LogUtil;

public class CourseService implements BaseCourseService {

	private LogUtil log;
	private BeanFactory factory;
	
	public CourseService() {
		log = UtilFactory.getLogUtil(getClass());
		factory = BeanFactory.getBeanFactory();
	}
	
	@Override
	public BaseList getAllBeanInfo() {
		// TODO Auto-generated method stub
		BaseList list = null;
		try{
			String[] fieldName = { CourseConstant.COURSE_ID };
			int[] status = { 1 };
			BaseDaoAdapter courseDAO = DaoFactory.getDaoByColName(CourseConstant.COLLECTION_NAME);
			list =  courseDAO.getAllBean(fieldName, status);
//			BaseDAO courseDAO = (BaseDAO) factory.getBean(BaseService.DAO_COURSE);		
//			list =  courseDAO.getAllBean();
		}catch(Exception e){
			log.info("调用CourseDAO的getAllBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}

		return list;
	}

	@Override
	public boolean insertBean(BaseObject object) {
		// TODO Auto-generated method stub
		try{
			BaseDaoAdapter courseDAO = DaoFactory.getDaoByColName(CourseConstant.COLLECTION_NAME);
			return courseDAO.insertBean(object);
		}catch(Exception e){
			log.info("调用CourseDAO的getAllBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}

	@Override
	public boolean deleteBean(String CourseId) {
		// TODO Auto-generated method stub
		try{
			BaseDaoAdapter courseDAO = DaoFactory.getDaoByColName(CourseConstant.COLLECTION_NAME);		
			return courseDAO.deleteBean(CourseId);
		}catch(Exception e){
			log.info("调用CourseDAO的deleteBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}

	@Override
	public boolean deleteBean(String[] CourseId) {
		// TODO Auto-generated method stub
		try{
			BaseDaoAdapter courseDAO = DaoFactory.getDaoByColName(CourseConstant.COLLECTION_NAME);		
			return courseDAO.deleteBean(CourseId);
		}catch(Exception e){
			log.info("调用CourseDAO的deleteBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}

	@Override
	public boolean updateBeanInfoById(String CourseId, BaseObject object) {
		// TODO Auto-generated method stub
		try{
			BaseDaoAdapter courseDAO = DaoFactory.getDaoByColName(CourseConstant.COLLECTION_NAME);		
			return courseDAO.updateBean(CourseId, object);
		}catch(Exception e){
			log.info("调用CourseDAO的updateBean方法发生错误");
			log.info(e.toString());
			e.printStackTrace();			
		}
		return false;
	}

	@Override
	public BaseObject getBeanInfoById(String beanId) {
		BaseObject bean = null;
		try{
			BaseDaoAdapter courseDAO = DaoFactory.getDaoByColName(CourseConstant.COLLECTION_NAME);		
			bean =  courseDAO.getBeanInfoById(beanId);
		}catch(Exception e){
			log.info("调用CourseDAO的getBeanInfoById方法发生错误,此时beanId为"+beanId);
			log.info(e.toString());
			e.printStackTrace();			
		}
		return bean;
	}

	@Override
	public BaseList getBeanInfoById(List IdList) {
		// TODO Auto-generated method stub
		if(IdList == null){
			return null;
		}
		BaseObject bean = null;
		BaseList list = (BaseList) factory.getBean(BaseBean.BEAN_LIST);
		String beanId = null;
		
		try{
			BaseDaoAdapter courseDAO = DaoFactory.getDaoByColName(CourseConstant.COLLECTION_NAME);
			
			for( int i = 0; i < IdList.size();i++ ) {
				bean =  courseDAO.getBeanInfoById((String) IdList.get(i));
				beanId = (String) bean.get(UserConstant.USER_ID);
				list.add(bean);
			}
			return list;
		}catch(Exception e){
			log.info("调用CourseDAO的getBeanInfoById方法发生错误,此时beanId为"+beanId);
			log.info(e.toString());
			e.printStackTrace();			
		}
		return null;
	}
}
